Skip to content

Fix #15307: Internal error for invalid code inside unknown macro argument - #9112

Open
ludviggunne wants to merge 2 commits into
cppcheck-opensource:mainfrom
ludviggunne:15307-syntax-err-unknown-macro
Open

ludviggunne wants to merge 2 commits into
cppcheck-opensource:mainfrom
ludviggunne:15307-syntax-err-unknown-macro

Conversation

@ludviggunne

Copy link
Copy Markdown
Collaborator

No description provided.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

I found no problems. The new check in findGarbageCode() turns the internalError "Cannot find end of expression" on F(a, { ...; return 0 }) into an unknownMacro error that names the right macro. It does this at file scope, inside function bodies, with extra arguments after the block and with nested calls. It did not fire on valid code: GCC statement expressions, lambdas inside initializer lists, braced initializer arguments and if ({...}).

One behaviour change is intended. In C files, where the old code was silently accepted with no diagnostic, the same invalid code now also gets unknownMacro. For example void f(){ F(a, { float y; return }); } printed nothing on base and now gives unknownMacro.

Testing

  • I built cppcheck and testrunner in pr/ and cppcheck in base/. Both builds succeeded.
  • ./testrunner TestTokenizer TestGarbage: 626 tests, 0 failures. The new unknownMacroBracedArgument test passes.
  • I compared PR and base on small C and C++ files in scratch/:
    • Base gives internalError: Cannot find end of expression and the PR gives unknownMacro for these C++ inputs:
      • F(a, { float y = p.y; return 0 });
      • F(a, { ... return 0 }, b);
      • void f(){ G(a, { ...; return 0 }) }
    • In C, base was silent on F(a, {...; return 0}), F({...}) and h(1, { int a; a }) nested in a call. The PR now reports unknownMacro for them, and it names the innermost macro.
    • These give no diagnostic on either version:
      • x = ({ int a = 1; a });
      • g(({ ... a })) and g(1, ({...}), 2)
      • g({ [](){ return 1; }(), 2 })
      • h(a, {1,2})
      • for (auto x : {1,2})
      • if ({ int a; a })
      • F(a, [x]{...})
      • F(a, {...; return 0;})
      • F(a, {...; if(y){} })
    • An unmatched paren still gives the same syntaxError as before.
  • I ran PR and base cppcheck on every file in test/cfg/*.c* and samples/. The counts of unknownMacro, internalError and syntaxError were the same on both.

Automated review of 771daae by Claude. The pull request was built and tested locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants